package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionConfigMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_UNION_WAR_UNION_BATTLE_REPORT_LIST,desc = "商会战况（从排行榜入口）")
public class CrossUnionWarBattleReportListCmd extends CrossUnionCommand {

    private int activityId;
    private String unionUid;
    private long unionServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionBattleReportListReqMsg reqMsg = UnionWarActivityServerProto.UnionBattleReportListReqMsg.parseFrom(packet.getBytes());
        activityId  = reqMsg.getActivityId();
        unionUid = reqMsg.getUnionUid();
        unionServerId = reqMsg.getUnionServerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(!CrossUnionWarActivityMgr.getOpenActivityMap().containsKey(activityId)){
            return;
        }
        Map<Integer, Map<Integer, List<CrossUnionWarDetail>>> detailMap = CrossUnionWarMgr.getUnionWarDetailMap().get(activityId);
        if(detailMap != null){
            List<CrossUnionWarDetail> list = new ArrayList<>();
            UnionWarActivityServerProto.UnionBattleReportListRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionBattleReportListRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setUnionUid(unionUid);
            CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, unionUid);
            if (null != simple){
                int crossServerCount = CrossUnionServerGroupMgr.getGroupServerCountByServerId(simple.getUnionBaseInfo().getServerId());
                int maxUnionMember = CrossUnionConfigMgr.getMaxMemberCount(simple.getUnionBaseInfo().getExperience(), simple.getUnionBaseInfo().isCross(), crossServerCount);
                respMsg.setMaxMemberCount(maxUnionMember);
            }
            respMsg.setRet(0);
            for(Map.Entry<Integer, Map<Integer, List<CrossUnionWarDetail>>> stageEntry : detailMap.entrySet()){
                for(Map.Entry<Integer, List<CrossUnionWarDetail>> roundEntry : stageEntry.getValue().entrySet()){
                    List<CrossUnionWarDetail> tempList = roundEntry.getValue();
                    int size = tempList.size();
                    for(int i = 0; i < size; i++){
                        CrossUnionWarDetail detail = tempList.get(i);
                        if(detail.getUnionUid().equals(unionUid)){
                            list.add(detail);
                        }
                    }
                }
            }
            build(respMsg, list);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_UNION_BATTLE_INFO, respMsg));
        }
    }

    private void build(UnionWarActivityServerProto.UnionBattleReportListRespMsg.Builder respMsg, List<CrossUnionWarDetail> detailList) {
        if (detailList != null) {
            for (CrossUnionWarDetail detail : detailList) {
                if(!respMsg.hasUnionInfo()){
                    CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId,detail.getUnionUid());
                    if(simple != null){
                        respMsg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(simple.getUnionBaseInfo()));
                    }
                }
                UnionWarActivityServerProto.UnionBattleReportTemp.Builder builder = UnionWarActivityServerProto.UnionBattleReportTemp.newBuilder();
                builder.setPhase(detail.getStageNo());
                builder.setSession(detail.getRoundNo());
                builder.setWinMark(detail.getWinMark());
                if(detail.getEnemyUnionUid() != null) {
                    CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, detail.getEnemyUnionUid());
                    if(simple != null) {
                        builder.setEnemyUnionInfo(UnionBasePb.parseUnionBaseTempMsg(simple.getUnionBaseInfo()));
                    }
                }
                respMsg.addReport(builder);
            }
        }
    }

    @Override
    public void sendData() {

    }
}
